package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.User;
import repository.UserDAO;

@WebServlet("/DoRegister")
public class DoRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public DoRegister() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out= response.getWriter();
		
		UserDAO userdao = null;
		try {
			userdao = new UserDAO();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(request.getParameter("userName"));
		User user = new User();
		user.setUserId(request.getParameter("userId"));
		user.setUserName(request.getParameter("userName"));
		user.setPwd(request.getParameter("pwd"));
		user.setPwd(request.getParameter("identity"));
		user.setPhnumber(request.getParameter("phnumber"));
		user.setAdamic(request.getParameter("adamic"));
		if(userdao.query(request.getParameter("userId"))!=null) {
			out.println("您已注册！3秒后进入登陆页面...");
			response.setHeader("Refresh", "3;url=login.jsp");
		}else {
			if(userdao.insert(user)!=0) {
				out.println("注册成功！3秒后进入登录页面...");
				response.setHeader("Refresh", "3;url=login.jsp");
			}else {			
				out.println("注册失败！3秒后返回注册页面...");
				response.setHeader("Refresh", "3;url=register.jsp");
			}
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
